﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Batalha_Estelar
{
    class Nave : GameObject
    {

        #region Fields

        protected float Velocidade;

        protected int vida = 3;

        public readonly List<Missel> Tanque = new List<Missel>();

        protected double tempo_aux = 0;

        protected const double atrasoTiro = 0.2f;

        public Vector2 Orientacao { get; set; }

        public bool HitFieldBounds { get; set; }

        #endregion

        #region Construtores

        public Nave(Game game, Vector2 posicao, Vector2 orientacao, Texture2D textura)
            : base(game)
        {
            Textura = textura;
            Posicao = posicao;
            Orientacao = orientacao;

            Direcao = new Vector2(0.0f, 0.0f);
            Velocidade = 250.0f;
            HitFieldBounds = false;
        }

        #endregion

        #region GameObject Methods

        public override void Update(GameTime gameTime)
        {
            controleNave(gameTime);
            controleMissel(gameTime);
            DestroyMissiles();
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Textura, Posicao, Color.White);

            if (Tanque.Count > 0)
            {
                foreach (Missel auxMissel in Tanque)
                {
                    auxMissel.Draw(spriteBatch);
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            //nada por enquanto...
            return;
        }

        #endregion

        #region Nave Methods

        private void controleNave(GameTime gameTime)
        {
            var config = GameConfig.GetCurrent();
            var controleTela = new ControleTela();
            var gameSpeed = config.GameSpeed;

            var newPosicao = Posicao + Direcao * Velocidade * gameSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (controleTela.EstaDentro(newPosicao, this.Textura.Bounds.Width, this.Textura.Bounds.Height))
            {
                this.Posicao = newPosicao;
                HitFieldBounds = false;
            }
            else
            {
                HitFieldBounds = true;
            }
        }

        private void controleMissel(GameTime gameTime)
        {
            if (Tanque.Count > 0)
            {
                foreach (Missel aux in Tanque)
                {
                    aux.Update(gameTime);
                }
            }
        }

        private void DestroyMissiles()
        {
            var missiles = Tanque.Where((x) => x.CanDestroy()).ToArray();
            
            foreach (var missel in missiles)
            {
                Tanque.Remove(missel);
            }
        }

        public void Atirar(GameTime gameTime, Vector2 posicaoNave)
        {
            double tempo = gameTime.TotalGameTime.TotalSeconds;
            
            if (tempo > tempo_aux)
            {
                posicaoNave.X += (this.Textura.Bounds.Width - 1) / 2;
                Missel missel = new Missel(this.MyGame, posicaoNave, Orientacao);
                Tanque.Add(missel);
                tempo_aux = gameTime.TotalGameTime.TotalSeconds + atrasoTiro;
            }
        }

        public void decrementarVida()
        {
            vida--;
        }

        public int getVida()
        {
            return vida;
        }

        public void resetarVida()
        {
            vida = 3;
        }

        #endregion

    }
}
